using UnityEngine;
using System.Collections;

public class HerderStateMachineScript : MonoBehaviour {
	
	CircleBehaviour circleBehaviour;
	
	HerdSteeringBehaviour convergeBehaviour; 	
	float convergeBlendWeight = 0.7f;
	
	public HerdBehaviour herdBehaviour; 	
	private float herdBlendWeight = 0.7f;

	
	//we should use enums to keep track of the states... because they are amazing :)
	public enum State
	{
   		Converge, Herd
	}
	
	
	// Use this for initialization
	void Start () 
	{
		//print("HerderStateMachineScript Start");
		
		circleBehaviour = (CircleBehaviour)gameObject.GetComponent("CircleBehaviour");
		convergeBehaviour = (ConvergeBehaviour)gameObject.GetComponent("ConvergeBehaviour");
		herdBehaviour = (HerdBehaviour)gameObject.GetComponent("HerdBehaviour");
	}
	
	// Update is called once per frame
	void Update () {}
	
	// returns the agents new state
	// takes a reference to the agent
	public State run (HerderBehaviourScript herder) 
	{
		//Make sure are behaviours are ready, this might get run before Start 
		if (!convergeBehaviour || !circleBehaviour || !herdBehaviour) 
			return State.Herd;
		switch ((int)herder.currentState) 
		{
			case (int)State.Converge:
				return convergeState(herder);
			
			case (int)State.Herd:
				return herdState(herder);
		}
		
		return State.Herd;
	}
	
	/* The State Methods
	 * 
	 * */
	
	// Converge on the captain ie mouse if in range
	private State convergeState(HerderBehaviourScript herder)
	{
		
		Vector3 direction = convergeBehaviour.getSteering(herder);
		
		herder.setDestination(direction);
		herder.ChangeColour(new Color(1,0,0));

		return State.Herd;
		
	}	
	
	
	
	private State herdState(HerderBehaviourScript herder)
	{
	
		herder.ChangeColour(new Color(1,1,0));

		herder.setDestination( herder.getClosestAgent().getPosition() );
		return State.Herd;
	}
	
	/*
	
	//
	// Returns true if a state transistion is required
	// @return bool
	//
	private bool transition_required()
	{
		bool required = false;
		
		if(this.agent.isFleeing())
		{
			required = true;
		}
		
		return required;
	}
	
	//
	// Returns the new state transistion
	// @return State (enum)
	//
	private State transition_state()
	{
		if(this.agent.isFleeing())
		{
			return State.Flee;
		}
		
		return State.Wander;
	}
	*/
}


